package com.phone.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import com.phone.entity.Admin;
import com.phone.entity.Product;
import com.phone.entity.Store;
import com.phone.entity.User;
import com.phone.service.ProductService;
import com.phone.service.StoreService;
import com.phone.service.UserService;

@Controller
public class ProductController {
	@Resource
	private UserService userServiceImpl;
	@Resource
	private ProductService productServiceImpl;
	@Resource
	private StoreService serviceimpl;
	
	@RequestMapping(value="allproduct")
	public String allproduct(Model model,HttpServletRequest request,HttpSession session){
		List<Product> products = productServiceImpl.allproduct();
		model.addAttribute("products", products);
		String username = request.getParameter("username");
		User user= userServiceImpl.seleu(username);
		Admin admin=userServiceImpl.selea(username);
		Store store=userServiceImpl.seles(username);
		if (user!=null) {
			session.setAttribute("user", user);
			return "/usershop.jsp";
		}else if (admin!=null) {
			session.setAttribute("admin", admin);
			return "/adminshop.jsp";
		}else if (store!=null) {
			session.setAttribute("store", store);
			return "/storeshop.jsp";
		}else {
			return "/shop.jsp";
		}
	}
	@RequestMapping(value="seleproduct")
	public String seleproduct(Model model,HttpServletRequest request){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		Product product = productServiceImpl.seleproduct(id);
		model.addAttribute("product", product);
		return "/details.jsp";
	}
	@RequestMapping(value="seeproduct")
	public String seeproduct(Model model,HttpServletRequest request){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		Product product = productServiceImpl.seleproduct(id);
		model.addAttribute("product", product);
//		User u=(User) session.getAttribute("user");
//		System.out.println(u);
//		String username = u.getUsername();
		String username=request.getParameter("username");
		User user= userServiceImpl.seleu(username);
		model.addAttribute("user", user);
		return "/userdetails.jsp";
	}
	@RequestMapping(value="seeeproduct")
	public String seeeproduct(Model model,HttpServletRequest request){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		Product product = productServiceImpl.seleproduct(id);
		model.addAttribute("product", product);
		String username=request.getParameter("username");
		Admin admin = userServiceImpl.selea(username);
		Store store=userServiceImpl.seles(username);
		if (admin!=null) {
			model.addAttribute("all", admin);
			return "/nodetails.jsp";
		}else {
			model.addAttribute("all", store);
			return "/nodetails.jsp";
		}
		
	}
	@RequestMapping("seproduct")
	public String seproduct(HttpSession session,HttpServletRequest request){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		Product product = productServiceImpl.seleproduct(id);
		session.setAttribute("product", product);
		return "/editproduct.jsp";
	}
	@RequestMapping(value="upproduct",method=RequestMethod.POST)
	public String upproduct(Model model,HttpSession session,HttpServletRequest request,Product product,MultipartFile img) throws IOException{
		    //获取文件名
			String filenames=img.getOriginalFilename();
			String filename=UUID.randomUUID().toString();
			if (!filenames.isEmpty()) {
				String name=filenames.substring(filenames.lastIndexOf("."));//获取拓展名
				img.transferTo(new File("E:\\JAVA开发软件包\\2017级java开发软件包\\新建文件夹\\PhoneStore\\WebContent\\images\\"+filename+name));
				product.setListimg(filename+name);
				productServiceImpl.upproduct(product);
				return "ssproduct";
			}else {
				String listimg=request.getParameter("listimg");
				product.setListimg(listimg);
				productServiceImpl.upproduct(product);
				return "ssproduct";
			}	
	}
	@RequestMapping(value="ssproduct",method=RequestMethod.POST)
	public String ssproduct(Model model,HttpSession session,HttpServletRequest request,Product product){
			Store s = (Store) session.getAttribute("store");
			String username=s.getUsername();
//		    String username = request.getParameter("username");
	        Store store=userServiceImpl.seles(username);
            int sid=store.getId();
			List<Product> products= serviceimpl.showproduct(sid);
			model.addAttribute("store", store);
			model.addAttribute("products", products);
			return "/storeshop2.jsp";
	}
	@RequestMapping("delproduct")
	public String delproduct(HttpServletRequest request,HttpSession session,Model model){
		String ids = request.getParameter("id");
		int id=Integer.parseInt(ids);
		Product seleproduct = productServiceImpl.seleproduct(id);
		String username=seleproduct.getStore().getUsername();
		Store store=userServiceImpl.seles(username);
		productServiceImpl.delproduct(id);
		int sid=store.getId();
		List<Product> products= serviceimpl.showproduct(sid);
		if (products==null || products.size()==0) {
			session.setAttribute("message", "你的商店没有东西啦");
			List<Product> product = productServiceImpl.allproduct();
			model.addAttribute("products", product);
			return "/storeshop.jsp";
		}else {
			session.setAttribute("store", store);
			session.setAttribute("products", products);
			return "/storeshop2.jsp";
		}
	}
}
